package cn.iocoder.yudao.module.shop.service.itemcount;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.itemcount.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemcount.ShopItemCountDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.itemcount.ShopItemCountConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.itemcount.ShopItemCountMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 项目计数 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopItemCountServiceImpl implements ShopItemCountService {

    @Resource
    private ShopItemCountMapper itemCountMapper;

    @Override
    public Long createItemCount(ShopItemCountCreateReqVO createReqVO) {
        // 插入
        ShopItemCountDO itemCount = ShopItemCountConvert.INSTANCE.convert(createReqVO);
        itemCountMapper.insert(itemCount);
        // 返回
        return itemCount.getId();
    }

    @Override
    public void updateItemCount(ShopItemCountUpdateReqVO updateReqVO) {
        // 校验存在
        validateItemCountExists(updateReqVO.getId());
        // 更新
        ShopItemCountDO updateObj = ShopItemCountConvert.INSTANCE.convert(updateReqVO);
        itemCountMapper.updateById(updateObj);
    }

    @Override
    public void update(ShopItemCountDO shopItemCountDO){
        itemCountMapper.updateById(shopItemCountDO);
    }

    @Override
    public void deleteItemCount(Long id) {
        // 校验存在
        validateItemCountExists(id);
        // 删除
        itemCountMapper.deleteById(id);
    }

    private void validateItemCountExists(Long id) {
        if (itemCountMapper.selectById(id) == null) {
            throw exception(ITEM_COUNT_NOT_EXISTS);
        }
    }

    @Override
    public ShopItemCountDO getItemCount(Long id) {
        return itemCountMapper.selectById(id);
    }

    @Override
    public ShopItemCountDO getItemCountByItemId(Long memberId, Long itemId){
        return itemCountMapper.selectOne(new LambdaQueryWrapperX<ShopItemCountDO>()
                .eq(ShopItemCountDO::getUserId, memberId)
                .eq(ShopItemCountDO::getItemId, itemId));
    }

    @Override
    public ShopItemCountDO create(Long memberId, Long itemId){
        ShopItemCountDO itemCount = new ShopItemCountDO();
        itemCount.setNum(0);
        itemCount.setItemId(itemId);
        itemCount.setUserId(memberId);

        itemCountMapper.insert(itemCount);

        return itemCount;
    }

    @Override
    public List<ShopItemCountDO> getItemCountList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return itemCountMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopItemCountDO> getItemCountPage(ShopItemCountPageReqVO pageReqVO) {
        return itemCountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopItemCountDO> getItemCountList(ShopItemCountExportReqVO exportReqVO) {
        return itemCountMapper.selectList(exportReqVO);
    }

}
